//
//  NSObject+IMG.m
//  UILbrary
//
//  Created by Apple on 2023/3/5.
//  Copyright © 2023 mac. All rights reserved.
//

#import "NSObject+IMG.h"

@implementation NSObject (IMG)


- (NSData *)compressImageToData:(UIImage *)image targetSize:(NSUInteger)targetSize {
    CGFloat compression = 1.0;
    NSData *imageData = UIImageJPEGRepresentation(image, compression);
    NSUInteger maxFileSize = targetSize; // specify the maximum file size you want here
    while ([imageData length] > maxFileSize && compression > 0.1) {
        compression -= 0.1;
        imageData = UIImageJPEGRepresentation(image, compression);
    }
    return imageData;
}


@end
